﻿@model Module
@{
    ViewBag.Title = "Module Details - " + Model.Title;
    var entry = ContextManager.CurrentDb.Entry(Model);
    entry.Reference(m => m.Venue).Load();
    entry.Collection(m => m.Speakers).Load();
    entry.Reference(m => m.Level).Load();
    entry.Collection(m => m.Feedbacks).Load();

    var registeredUsers = Model.RegisteredUsers().OrderBy(r => r.LastName).ThenBy(r => r.FirstName).ToList();
    var feedback = Model.Feedbacks.OrderByDescending(f => f.DatePosted).ToList();
    
    var regUserMap = registeredUsers.ToDictionary(u => u.Interop.Id);
}
<div class="mainDetails">
    @Html.Raw(Model.Description)
    <h3>Registered Users</h3>
    @if (registeredUsers.Count > 0)
    {
        <p>@registeredUsers.Count users registered out of @Model.MaxEnrollment. @Html.ActionLink("Email all registered users", "EmailUsers", new { id = Model.Id }).</p>
        @Html.Partial("ListOfUserProfile", registeredUsers)
    }
    else
    {
        <p>There are no registered users for this module.</p>
    }
    <h3>Feedback</h3>
    @if (feedback.Count > 0)
    {
        <table class="wide">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Date Posted</th>
                    <th>Comments</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var f in feedback)
                {
                    var u = regUserMap[f.UserInteropId];
                    <tr>
                        <td>@u.NameFormatted()</td>
                        <td><a href="mailto:@u.EmailAddress">@u.EmailAddress</a></td>
                        <td>@f.DatePosted.ToShortDateString()</td>
                        <td>@f.Comments</td>
                    </tr>   
                }
            </tbody>
        </table>   
    }
    else 
    {
        <p>There is no feedback for this module.</p>
    }    
</div>
<div class="rightDetails">
    @{ Html.RenderPartial("ModuleRightBar", Model); }
</div>